
#include <cmath>

#include "thread_pool.hpp"

void * func () {

    // std::printf ("Main count = %d\n", *static_cast<int *> (_args));
    // std::this_thread::sleep_for (std::chrono::microseconds {500000});
    volatile long double dummy = 0;
    for (int i = 0; i < 10000000; ++i)
        dummy = dummy + std::sin (i);

    return nullptr;
}

void * callback (void * _args) {

    if (!_args)
        return nullptr;

    // std::printf ("CallBack count = %d\n", *static_cast<int *> (_args));

    return _args;
}

int main (const int _argc, const char * const _argv[], const char * const _envp[]) {


    // while (1)
    {

        auto _start = GetThreadCpuTimeUs ();
        func ();
        auto _end = GetThreadCpuTimeUs ();
        std::printf ("func cost = %ld\n", _end - _start);
    }

    Thread_Pool_ct pool {

            Thread_Pool_ct::Pool_Config_st {
                    ._queue_max      = 200,
                    ._thread_max     = 8,
                    ._thread_min     = 2,
                    ._thread_step    = 1,
                    ._auto_threshold = false,
            }

    };

    std::printf ("start = %d\n", pool.start ());

    int id = 100, priority = 1000, count = 0;
    while (count != 10000) {
        // std::this_thread::sleep_for (std::chrono::seconds (1));

        pool.task_add (

                Thread_Pool_ct::Task_Builder_st {}
                        .id (static_cast<std::uint64_t> (id++))
                        .priority (static_cast<std::uint64_t> (priority++))
                        .name ("Hello")
                        .entry (func)
                        .callback (callback)
                        .build ()

        );


        if (count++ % 10 == 0)
            pool.print_state ();
    }

    pool.stop ();
    pool.print_state ();

    return 0;
}
